package com.lzh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.bean.*;
import com.lzh.common.bean.PageBean;
import com.lzh.common.bean.ResultBean;
import com.lzh.common.curd.impl.BaseServiceImpl;
import com.lzh.common.ex.BusinessException;
import com.lzh.common.utils.Assert;
import com.lzh.mapper.SupplierContactMapper;
import com.lzh.mapper.SupplierMapper;
import com.lzh.mapper.SupplierPayformationMapper;
import com.lzh.mapper.UserMapper1;
import com.lzh.query.SupplierContactQuery;
import com.lzh.service.SupplierSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cn.hutool.json.XMLTokener.entity;

@Service
public class SupplierServiceImpl extends BaseServiceImpl<Supplier, SupplierMapper> implements SupplierSerivce {

    @Autowired
    UserMapper1 userMapper1;

    @Autowired
    SupplierContactMapper supplierContactMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    SupplierPayformationMapper supplierPfMapper;

    /**
     * 根据id查找供应商相关信息
     * @param id
     * @return
     */
    public Supplier querySupplierById(Integer id) {

        //查找供应商
        Supplier supplier = supplierMapper.get(id);

        //查找供应商联系人
        SupplierContact supplierContact = supplierContactMapper.queryById(id);
        supplier.setContact(supplierContact);

        //查找供应商联系人信息
        SupplierPayformation supplierPayformation = supplierPfMapper.queryById(id);
        supplier.setSupplierPayformation(supplierPayformation);


        return supplier;
    }

    /**
     * 保存用户
     * @param supplier
     * @return
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Supplier supplier) {
        //验证是否存在
        int i = userMapper1.queryUserByUserName(supplier.getAccount());
        //保存的时候如果他的逻辑删除是1那么覆盖掉数据
//        User1 user1 = userMapper1.queryByUserName(supplier.getAccount());
//        if (user1.getIsdel() == 1){
//            int i1 = userMapper1.deleteById(user1.getId());
//        }
        if(i>0)
            throw new BusinessException("该账号已存在！");
        //保存用户
        User1 user=new User1(supplier.getAccount(),supplier.getPassword(), UserType.SUPPLIER_USER);
        int rs=userMapper1.save(user);
        Assert.isZerp(rs,"供应商用户保存失败");


        //设置连接人的供应商id
        supplier.getContact().setSupplierId(user.getId());

        //保存供应商联系人
        int rs1 = supplierContactMapper.save(supplier.getContact());
        Assert.isZerp(rs1,"保存供应商联系人失败");

        //保存银行信息
        supplier.getSupplierPayformation().setSupplierId(user.getId());
        int save = supplierPfMapper.save(supplier.getSupplierPayformation());
        Assert.isZerp(save,"保存供应商收付款信息失败");

        //设置供应商id
        supplier.setId(user.getId());
        supplier.setAccount(user.getAccount());
        supplier.setPassword(user.getPassword());
        int save1 = supplierMapper.save(supplier);
        int i1 = supplierMapper.saveSupplierCategory(user.getId(), supplier.getProductType());
        return 1;
    }


    /**
     * 更新供应商信息
     * @param supplier
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateSupplier(Supplier supplier) {
        //更新用户
        //验证 账号密码不允许为空
        if (supplier.getAccount()==null || supplier.getPassword()==null){
            throw new BusinessException("账号密码不允许为空");
        }

        //更新供应商用户表
        int update = userMapper1.update(supplier.getAccount(), supplier.getPassword(), supplier.getId());
        
        //更新供应商信息
        int update1 = supplierMapper.update(supplier);
        supplier.getContact().setSupplierId(supplier.getId());
        //更新供应商联系人
        SupplierContact contact = supplier.getContact();
        int update2 = supplierContactMapper.update(contact);

        //更细收付款信息
        supplier.getSupplierPayformation().setSupplierId(supplier.getId());
        int update3 = supplierPfMapper.update(supplier.getSupplierPayformation());
        int all = update1 + update2 + update+update3;

        return all;
    }



    /**
     * 查询供应商和联系人信息
     * @param query
     * @return
     */
    public PageBean<Supplier> query(SupplierContactQuery query) {
        //分页查询供应商信息
        PageHelper.startPage(query.getPage(), query.getPageSize());//后面紧跟查询
        List list = baseMapper.query(query);
        PageInfo info = new PageInfo(list);

        //查询供应商联系人信息
      return new PageBean(query.getPage(), query.getPageSize(), info.getTotal(), list);
    }

    /**
     * 删除供应商和联系人信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int delete(int id){

           //删除供应商用户

           int i = userMapper1.deleteById(id);
           if (i <= 0) throw new BusinessException("删除失败或删除账号不存在");

           //删除供应商联系人
           int delete1 = supplierMapper.delete(id);
           Assert.isZerp(delete1, "删除供应商联系人失败");
           //删除供应商
           int delete = supplierContactMapper.delete(id);
           Assert.isZerp(delete, "删除供应商失败");
           return i + delete1 + delete;

    };

    /**
     * 重置密码
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int resetPassword(int id) {
        //首先重置user表中的密码
        int i = userMapper1.resetPassword(id);
        //如果重置成功继续重置
        if (i==0) throw new BusinessException("重置密码失败");

        //重置供应商表中的密码
        int i1 = supplierMapper.resetPassword(id);
        int all = 1+i1;
        return all;
    }
}
